package com.capgemini.fs.presentation.user.web.action;

import java.util.Iterator;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.capgemini.fs.app.ekms.model.Group;
import com.capgemini.fs.app.ekms.model.Role;
import com.capgemini.fs.app.ekms.model.User;
import com.capgemini.fs.app.ekms.service.GroupService;
import com.capgemini.fs.app.ekms.service.RoleService;
import com.capgemini.fs.app.ekms.service.UserService;

public class UserListAction extends Action {
	private UserService userListUserService;
	private GroupService userListGroupService;
	private RoleService userListRoleService;

	public UserService getUserListUserService() {
		return userListUserService;
	}

	public void setUserListUserService(UserService userListUserService) {
		this.userListUserService = userListUserService;
	}

	public GroupService getUserListGroupService() {
		return userListGroupService;
	}

	public void setUserListGroupService(GroupService userListGroupService) {
		this.userListGroupService = userListGroupService;
	}

	public RoleService getUserListRoleService() {
		return userListRoleService;
	}

	public void setUserListRoleService(RoleService userListRoleService) {
		this.userListRoleService = userListRoleService;
	}

	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {

		JSONArray jsonArray = new JSONArray();
		try {
			// add role
			List<Role> roleList = userListRoleService.getAllRoles();
			System.out.println(roleList.size());
			HttpSession session = request.getSession();
			session.setAttribute("roleList", roleList);
			// add group
			List<Group> groupList = userListGroupService.findAllGroup();
			session.setAttribute("groupList1", groupList);
			// add user
			List<User> userList = userListUserService.getAllUsers();

			for (int i = 0; i < userList.size(); i++) {
				User u = userList.get(i);

				JSONObject jo = new JSONObject();

				jo.put("id", u.getId());
				jo.put("userName", u.getUserName());
//				jo.put("firstName", u.getFirstName());
//				jo.put("lastName", u.getLastName());
				jo.put("email", u.getEmail());
				// jo.put("userCategory", u.getMemberType());
				jo.put("active", u.getStatus());
				// jo.put("phoneNo", u.getPhoneNo());
				// add group info
				String joGroups = "";
				if (u.getGroups() == null) {

				} else {
					Iterator<Group> groups = u.getGroups().iterator();

					while (groups.hasNext()) {
						Group joGroup = groups.next();
						joGroups += joGroup.getGroupName() + "/ ";
					}
				}
				if (joGroups.length() > 2) {
					joGroups = joGroups.substring(0, joGroups.length() - 2);
				}
				// add role info
				Iterator<Role> roles = u.getRoles().iterator();
				String joRoles = "";
				while (roles.hasNext()) {
					Role joRole = roles.next();
					joRoles += joRole.getName() + "/ ";
				}
				if (joRoles.length() > 2) {
					joRoles = joRoles.substring(0, joRoles.length() - 2);
				}
				jo.put("roles", joGroups);
				jo.put("groups", joRoles);

				jsonArray.add(jo);
			}

			request.setAttribute("userList", jsonArray);
			return mapping.findForward("suc");
		} catch (Exception e) {
			e.printStackTrace();
			return mapping.findForward("failed");
		}

	}

}
